package ie.lawlor.amvc.config;

import ie.lawlor.amvc.event.EventName;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;


class AppEventDefinitionParser implements BeanDefinitionParser {

    public BeanDefinition parse(Element element, ParserContext parserContext) {
        BeanDefinitionRegistry registry = parserContext.getRegistry();
        String beanName = element.getAttribute(AbstractTriadDefinitionParser.ID);
        BeanDefinition appEventDefinition = createAppEventBeanDefinition(element);
        registry.registerBeanDefinition(beanName, appEventDefinition);
        return appEventDefinition;
    }

    private BeanDefinition createAppEventBeanDefinition(Element element) {
        RootBeanDefinition appEventBeanDefinition = new RootBeanDefinition(
                EventName.class);
        MutablePropertyValues propertyValues = appEventBeanDefinition
                .getPropertyValues();
        propertyValues.addPropertyValue(AbstractTriadDefinitionParser.NAME, 
        		element.getAttribute(AbstractTriadDefinitionParser.NAME));
        propertyValues.addPropertyValue(AbstractTriadDefinitionParser.NAMESPACE, 
        		AbstractTriadDefinitionParser.APPLICATION_NAMESPACE);
        appEventBeanDefinition.setPropertyValues(propertyValues);
        return appEventBeanDefinition;
    }
}